package org.yinweichen.llm_dataset_backend.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yinweichen.llm_dataset_backend.DTO.PointsCreateDTO;
import org.yinweichen.llm_dataset_backend.DTO.PointsKeywordUpdateDTO;
import org.yinweichen.llm_dataset_backend.DTO.PointsUpdateDTO;
import org.yinweichen.llm_dataset_backend.entity.Points;
import org.yinweichen.llm_dataset_backend.entity.StandardAnswer;
import org.yinweichen.llm_dataset_backend.entity.StandardAnswerId;
import org.yinweichen.llm_dataset_backend.repository.PointsRepository;
import org.yinweichen.llm_dataset_backend.service.PointsService;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class PointsServiceImpl implements PointsService {
    private final PointsRepository pointsRepository;

    @Override
    public ResponseEntity<List<Points>> getPointsByAnswerId(Long questionId, String version) {
        List<Points> points = pointsRepository.findByAnswerIdAndActiveTrue(questionId, version);
        return ResponseEntity.ok(points);
    }

    @Override
    @Transactional
    public ResponseEntity<Points> createPoint(PointsCreateDTO pointsCreateDTO) {
        Points point = Points.builder()
                .keyword(pointsCreateDTO.getKeyword())
                //TODO: 这里需要先获取答案，或者构造一个工具实例
                .answer(StandardAnswer.builder()
                        .questionId(pointsCreateDTO.getAnswerId())
                        .version(pointsCreateDTO.getVersion())
                        .build())
                .value(pointsCreateDTO.getValue())
                .active(true)
                .build();
        Points savedPoint = pointsRepository.save(point);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedPoint);
    }

    @Override
    @Transactional
    public ResponseEntity<Void> inactivatePoint(Long questionId, String version, Long pointId) {
        pointsRepository.findById(pointId)
            .ifPresent(point -> {
                point.setActive(false);
                pointsRepository.save(point);
            });
        return ResponseEntity.noContent().build();
    }

    @Override
    @Transactional
    public ResponseEntity<Void> activatePoint(Long questionId, String version, Long pointId) {
        pointsRepository.findById(pointId)
            .ifPresent(point -> {
                point.setActive(true);
                pointsRepository.save(point);
            });
        return ResponseEntity.noContent().build();
    }

    @Override
    @Transactional
    public ResponseEntity<Points> updateValue(PointsUpdateDTO pointsUpdateDTO) {
        Points point = pointsRepository.findById(pointsUpdateDTO.getId())
            .orElseThrow(() -> new IllegalArgumentException("Point not found"));

        int updatedRows = pointsRepository.updateValue(
            pointsUpdateDTO.getId(),
            pointsUpdateDTO.getValue()
        );
        if (updatedRows == 0) {
            throw new RuntimeException("Failed to update Points importance level");
        }
        return ResponseEntity.ok(pointsRepository.findById(pointsUpdateDTO.getId()).get());
    }

    @Override
    @Transactional
    public ResponseEntity<Points> updateKeyword(PointsKeywordUpdateDTO pointsKeywordUpdateDTO) {
        Points point = pointsRepository.findById(pointsKeywordUpdateDTO.getId())
            .orElseThrow(() -> new IllegalArgumentException("Point not found"));

        int updatedRows = pointsRepository.updateKeyword(
            pointsKeywordUpdateDTO.getId(),
            pointsKeywordUpdateDTO.getKeyword()
        );
        if (updatedRows == 0) {
            throw new RuntimeException("Failed to update Points keyword");
        }
        return ResponseEntity.ok(pointsRepository.findById(pointsKeywordUpdateDTO.getId()).get());
    }
}
